package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.player.CrossUserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C_UNION_CREATE, desc = "创建联盟")
public class CrossCreateUnionCmd extends CrossCommand {

    long playerId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {

        playerId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        UnionProto.CrossUnionCreateReqMsg reqMsg = UnionProto.CrossUnionCreateReqMsg.parseFrom(packet.getBytes());
        String unionName = reqMsg.getUnionName();
        int unionFlag = reqMsg.getUnionFlag();
        String outNotice = reqMsg.getOutNotice();
        String qqCode = reqMsg.getQqCode();
        String weChatCode = reqMsg.getWechatCode();
        boolean isAllowRandomJoin = reqMsg.getIsAllowRandomJoin();
        String innerNotice = reqMsg.getInnerNotice();

        //重要
        CrossUserMgr.updateUserBaseInfo(playerId, PlayerBasePb.parseToUserBaseInfo(reqMsg.getPlayerBaseData()));

        CrossUserBaseInfo player = CrossUserMgr.getCrossUserBaseInfo(playerId);
        ret = CrossUnionMgr.createUnion(player, unionName, isAllowRandomJoin, outNotice, weChatCode, qqCode, innerNotice, unionFlag);
    }

    @Override
    public void sendData() {
        if(ret != 0) {
            UnionProto.UnionCreateRespMsg.Builder respMsg = UnionProto.UnionCreateRespMsg.newBuilder();
            respMsg.setRet(ret);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_UNION_CREATE, respMsg));
        }
    }
}
